package com.micromall.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.micromall.common.utils.StringUtils;
import com.micromall.mapper.BusiProductMapper;
import com.micromall.model.BusiProduct;
import com.micromall.service.IBusiProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author Dante Fung
 * @since 2017-07-26
 */
@Service
public class BusiProductServiceImpl extends ServiceImpl<BusiProductMapper, BusiProduct> implements IBusiProductService {


        private static final Logger logger = LoggerFactory.getLogger(BusiProductServiceImpl.class);

        @Autowired
        public JdbcTemplate jdbcTemplate;

        public List<BusiProduct> searchList(String searchName,String type){
            StringBuffer sql = new StringBuffer();
            StringBuilder andClause = new StringBuilder();
            sql.append("select * from BUSI_PRODUCT bp where 1=1 ");

            //数据状态有效且是已发布
            sql.append(" and bp.DATA_STATUS = 'Y' ");
            sql.append("and bp.STANDY1 = 'Y'");
            //模糊查询
            if(StringUtils.isNotBlank(searchName)){
                andClause.append("and bp.ZH_NAME like '%"+searchName+"%'");
            }
            //排序
            if (StringUtils.isNotBlank(type)){
                if( "默认".equalsIgnoreCase(type)){
                    type = "CREATE_TIME";
                    andClause.append("ORDER BY bp.create_time desc");
                }
                if( "价格".equalsIgnoreCase(type)){
                    type = "PRICE";
                    andClause.append("ORDER BY bp.price desc");
                }
            }else{
                //默认按创建时间排序
                type = "CREATE_TIME";
                andClause.append("ORDER BY bp.create_time desc");
            }

            sql.append(andClause);
            logger.info(String.format("最终的查询sql为:%s", sql.toString()));
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sql.toString());
            List<BusiProduct> busiProducts = new ArrayList<>();
            //遍历List，把map的对象属性set入实体，在放入List
            for (Map<String ,Object> map :list) {
                BusiProduct busiProduct = new BusiProduct();
                busiProduct.setId(map.get("ID")+"");
                busiProduct.setProdCode(map.get("PROD_CODE")+"");
                busiProduct.setZhName(map.get("ZH_NAME")+"");
                busiProduct.setEnName(map.get("EN_NAME")+"");
                busiProduct.setImgUrl(map.get("IMG_URL")+"");
                busiProduct.setProdDesc(map.get("PROD_DESC")+"");
                busiProduct.setCategory(map.get("CATEGORY")+"");
                busiProduct.setDataStatus(map.get("DATA_STATUS")+"");
                busiProduct.setPublishStatus(map.get("PUBLISH_STATUS")+"");
                busiProduct.setCreateTime(map.get("CREATE_TIME")+"");
                busiProduct.setCreator(map.get("CREATOR")+"");
                busiProduct.setCreatorName(map.get("CREATOR_NAME")+"");
                busiProduct.setUpdator(map.get("UPDATOR")+"");
                busiProduct.setUpdateTime(map.get("UPDATE_TIME")+"");
                busiProduct.setDataFrom(map.get("DATA_FROM")+"");
                busiProduct.setPriceId(map.get("PRICE_ID")+"");
                busiProduct.setPrice(map.get("PRICE")+"");
                busiProduct.setDiscount(map.get("DISCOUNT")+"");
                busiProduct.setDisPrice(map.get("DIS_PRICE")+"");
                busiProduct.setMemo(map.get("MEMO")+"");
                busiProduct.setStandy1(map.get("STANDY1")+"");
                busiProduct.setStandy2(map.get("STANDY2")+"");
                busiProduct.setStandy3(map.get("STANDY3")+"");
                busiProduct.setStandy4(map.get("STANDY4")+"");
                busiProduct.setStandy5(map.get("STANDY5")+"");
                busiProducts.add(busiProduct);
            }
            return busiProducts;
        }
}
